home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / BlueBox Spy / Blue Box Daemon / source / CTCPServerThread.cp < prev    next >
Encoding:
Text File  |  1998-08-10  |  3.1 KB  |  126 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CTCPServerThread.cp            ©1995-1998 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4.  
  5. #include "CTCPServerThread.h"
  6. #include "CTCPResponder.h"
  7.  
  8.  
  9. // ---------------------------------------------------------------------------
  10. //        • ~CTCPServerThread
  11. // ---------------------------------------------------------------------------
  12.  
  13.  
  14. CTCPServerThread::CTCPServerThread(
  15.     UInt16                            inMaxConnections,
  16.     UInt16                            inLocalPort,
  17.     PP_PowerPlant::LTCPEndpoint*    inNetworkEndpoint,
  18.     CSimpleTCPServer*                inServerMaster)
  19.         : LThread(    false,
  20.                     PP_PowerPlant::thread_DefaultStack, 
  21.                     PP_PowerPlant::LThread::threadOption_Default,
  22.                     nil),
  23.           mEndpoint(inNetworkEndpoint),
  24.           mMaxConnections(inMaxConnections),
  25.           mPort(inLocalPort),
  26.           mServerMaster(inServerMaster)
  27. {
  28.     mDisconnectReceived = mStartDisconnect = false;
  29. }
  30.  
  31.  
  32. // ---------------------------------------------------------------------------
  33. //        • ~CTCPServerThread
  34. // ---------------------------------------------------------------------------
  35.  
  36. CTCPServerThread::~CTCPServerThread()
  37. {
  38. }
  39.  
  40. // ---------------------------------------------------------------------------
  41. //        • StartDisconnect
  42. // ---------------------------------------------------------------------------
  43.  
  44. void
  45. CTCPServerThread::StartDisconnect()
  46. {
  47.     if (!mStartDisconnect) {
  48.         mStartDisconnect = true;
  49.         
  50.         if (mState == PP_PowerPlant::LThread::threadState_Waiting)
  51.             ResumeServerThread();
  52.         else
  53.             mEndpoint->AbortThreadOperation(this);
  54.     }
  55. }
  56.  
  57. // ---------------------------------------------------------------------------
  58. //        • SuspendThread
  59. // ---------------------------------------------------------------------------
  60.  
  61. void
  62. CTCPServerThread::SuspendServerThread()
  63. {
  64.     mConnectionSignal.Wait();
  65. }
  66.  
  67. // ---------------------------------------------------------------------------
  68. //        • ResumeThread
  69. // ---------------------------------------------------------------------------
  70.  
  71. void
  72. CTCPServerThread::ResumeServerThread()
  73. {
  74.     mConnectionSignal.Signal();
  75. }
  76.  
  77. // ---------------------------------------------------------------------------
  78. //        • Run
  79. // ---------------------------------------------------------------------------
  80.  
  81. void*
  82. CTCPServerThread::Run()
  83. {
  84.     try {
  85.         PP_PowerPlant::LInternetAddress address(0, mPort);
  86.         mEndpoint->Bind(address, mMaxConnections);
  87.         mServerMaster->BindCompleted();
  88.  
  89.         // Endless loop: watch and wait for incomming connections.
  90.         while (1) {
  91.             try {
  92.                 SuspendServerThread();    //Nothing to do but wait for T_LISTEN
  93.  
  94.                 if (mStartDisconnect)
  95.                     break;
  96.                 
  97.                 mEndpoint->Listen();
  98.                 
  99.                 if (mServerMaster->mConnectionCount < mMaxConnections) {
  100.                     CTCPResponder* theConnection = new CTCPResponder(mServerMaster->GetSuperCommander());
  101.                     theConnection->Accept(mServerMaster);
  102.                 } else {
  103.                     mEndpoint->RejectIncoming();
  104.                     mServerMaster->IncRejectionCount();
  105.                 }
  106.                 
  107.             }
  108.             catch(...) {
  109.             }
  110.         }
  111.     }
  112.     catch(...) {
  113.     }
  114.  
  115.     // We're done with the connection… bail out.
  116.     
  117.     try {
  118.         mEndpoint->Unbind();
  119.     }
  120.     catch(...) {
  121.     }
  122.  
  123.     mServerMaster->ServerThreadDied();
  124.     return nil;
  125. }
  126.